package com.avp;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.media.*;
import android.net.*;
import android.os.*;
import android.util.Log;
import android.view.*;
import android.view.View.*;
import android.view.View;
import android.widget.*;
import java.io.*;
import java.net.*;
import java.util.*;
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.Toast;  
import android.view.View.OnClickListener;
import android.os.Environment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main extends Activity 
{	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		//create new ringer server

		
		Button login;
		final EditText serverIP;
		final EditText portNum;
		final EditText username;
		
		super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        login = (Button) findViewById(R.id.button1);
        serverIP = (EditText)findViewById(R.id.editText3);
        portNum = (EditText)findViewById(R.id.editText2);
        username = (EditText)findViewById(R.id.editText1);
        
       // Intent i = new Intent(Main.this,Activity2.class);
        
        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
            	String file = "uInfo";
            	//DirectoryClient.disconnect();
            	String uname;
            	int pNum;
            	String sIP;
            	//if(file.length()==0){
            	//Should probably have a better input checking than this.
            	try
            	{
            		sIP = serverIP.getText().toString();
                	pNum =  Integer.parseInt(portNum.getText().toString());
            		uname = username.getText().toString();
            	}
            	catch(Exception e)
            	{
            	Toast.makeText(getApplicationContext(), "Error in input",
    		                Toast.LENGTH_SHORT).show();
            	return;
            	}
            		if (!DirectoryClient.connect(sIP,pNum,uname))
            		{
            			Toast.makeText(getApplicationContext(), "Error with server",
        		                Toast.LENGTH_SHORT).show();
            			return;
            		}
            	//}
            	//else{
            		/*sIP=
            		pNum=
            		uname=
            		DirectoryClient.connect(sIP,pNum,uname);*/
            	//}
            	
        		
            	
            	
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                
                startActivityForResult(myIntent, 0);
                
                try {
                    // Write 20 Strings
                    DataOutputStream out = 
                            new DataOutputStream(openFileOutput(file, Context.MODE_PRIVATE));
                    for (int i=0; i<20; i++) {
                        out.writeUTF(Integer.toString(i));
                    }
                    out.close();

                    // Read them back
                    DataInputStream in = new DataInputStream(openFileInput(file));
                    try {
                        for (;;) {
                          Log.i("Data Input Sample", in.readUTF());
                        }
                    } catch (EOFException e) {
                        Log.i("Data Input Sample", "End of file reached");
                    }
                    in.close();
                } catch (IOException e) {
                    Log.i("Data Input Sample", "I/O Error");
                }
                
            }

        });
	}

}